
package com.jiang.droidsms.ui.adapter;

import com.jiang.droidsms.R;
import com.jiang.droidsms.data.SMSTable.SMS;
import com.jiang.droidsms.data.SMSTable.Threads;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

/**
 * The Class ThreadListAdapter.
 */
public class ThreadListAdapter extends CursorAdapter {

    /** The m context. */
    Context mContext;

    /**
     * Instantiates a new thread list adapter.
     * 
     * @param con the con
     * @param cursor the cursor
     */
    public ThreadListAdapter(Context con, Cursor cursor) {
        super(con, cursor, true);
        mContext = con;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(mContext).inflate(R.layout.threads_adapter,
                                                     null);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        view.setBackgroundResource(R.drawable.list_bg_mid);
        int threadId = cursor.getInt(cursor.getColumnIndex(Threads._ID));

        Cursor smsCur = mContext.getContentResolver().query(
                                                            SMS.CONTENT_URI,
                                                            null,
                                                            SMS.THREAD_ID + "="
                                                                    + threadId,
                                                            null, null);
        String name = "";
        if (smsCur.moveToFirst()) {
            name = smsCur.getString(smsCur.getColumnIndex(SMS.ADDRESS));
            Cursor phoneCursor = null;
            // ���������7λ����ȡ���7λ��ƥ�䣬�����Ҫ�ϸ����
            if (name.length() >= 7) {
                phoneCursor = mContext.getContentResolver()
                        .query(
                               Phone.CONTENT_URI,
                               new String[] {
                                       Phone.CONTACT_ID, Phone.DISPLAY_NAME,
                                       Phone.PHOTO_ID
                               },
                               Phone.NUMBER + " LIKE '%"
                                       + name.substring(name.length() - 7)
                                       + "'", null, null);
            } else {
                phoneCursor = mContext.getContentResolver()
                        .query(
                               Phone.CONTENT_URI,
                               new String[] {
                                       Phone.DISPLAY_NAME, Phone.DISPLAY_NAME,
                                       Phone.PHOTO_ID
                               }, Phone.NUMBER + "=" + name, null, null);
            }
            if (phoneCursor.moveToFirst()) {
                // TODO: ȡ����ϵ��ͷ���ܣ�Ч��̫�ͣ��ݲ��ṩ
                // int photoId = phoneCursor.getInt(phoneCursor
                // .getColumnIndex(Phone.PHOTO_ID));
                // Cursor photoCursor = mContext.getContentResolver().query(
                // Data.CONTENT_URI, new String[] { Data.DATA15 },
                // Data._ID + "=" + photoId, null, null);
                // if (photoCursor.moveToFirst()) {
                // byte[] data = photoCursor.getBlob(photoCursor
                // .getColumnIndex(Data.DATA15));
                // if (data != null && data.length != 0) {
                // Drawable photo = Drawable.createFromStream(
                // new ByteArrayInputStream(data), "photo");
                // if (photo != null) {
                // ((ImageView)
                // view.findViewById(R.id.avatar)).setImageDrawable(photo);
                // }
                // }
                // }
                // photoCursor.close();
                name = phoneCursor.getString(phoneCursor
                        .getColumnIndex(Phone.DISPLAY_NAME));
            }
            phoneCursor.close();
        }

        int count = cursor.getInt(cursor.getColumnIndex(Threads.MESSAGE_COUNT));
        String newSms = "";
        Cursor newCur = mContext.getContentResolver()
                .query(
                       SMS.CONTENT_URI,
                       null,
                       SMS.THREAD_ID + "=" + threadId + " AND " + SMS.READ
                               + "=" + "0", null, null);
        if (newCur.moveToFirst()) {
            newSms = "new (" + newCur.getCount() + ")";
        }

        ((TextView) view.findViewById(R.id.name)).setText(name + "  (" + count
                + ") " + newSms);
    }
}
